package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
public class CountdownTask {

    @Autowired
    private ThreadPoolExecutor pool;

    @PostConstruct
    public void start() {
        pool.execute(() -> {
            int left = 60; // 倒计时 60 秒
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss");
            while (left >= 0) {
                System.out.println(LocalTime.now().format(fmt) +
                        "  倒计时剩余 " + left-- + " 秒");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException ignored) {
                    Thread.currentThread().interrupt();
                }
            }
            System.out.println("倒计时结束！");
        });
    }
}